<?php
/**
 * $Id: helper.php 7 2010-04-03 17:44:17Z webbochsant $
 *  @package	Joomla
 *  @author Daniel Eliasson
* 	@copyright	Copyright (C) 2010 Stilero. All rights reserved.
* 	@license	GNU/GPL, see LICENSE.php
* 	Joomla! is free software. This version may have been modified pursuant
* 	to the GNU General Public License, and as distributed it includes or
* 	is derivative of works licensed under the GNU General Public License or
* 	other free or open source software licenses.
*/

// no direct access
defined('_JEXEC') or die('Restricted access'); 

require_once (JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');


class modVirtuemartLatestProductsXHTMLHelper{
	
	function getProducts($params){
		
		$max_items = $params->get( 'max_items', 3 ); //maximum number of items to display
		//$category_id = $params->get( 'category_id', null ); // Display products from this category only
		$table_prefix = $params->get( 'table_prefix', "vm" ); // Display the "Add-to-Cart" Link?
		


		$db		=& JFactory::getDBO();
		$result	= null;
	
		
		$query  = "SELECT #__".$table_prefix."_product.*, #__".$table_prefix."_tax_rate.tax_rate, #__".$table_prefix."_product_discount.*, #__".$table_prefix."_product_price.product_price "
			."FROM #__".$table_prefix."_product "
			."LEFT JOIN #__".$table_prefix."_product_price "
			."ON #__".$table_prefix."_product.product_id = #__".$table_prefix."_product_price.product_id "
			."LEFT OUTER JOIN #__".$table_prefix."_tax_rate "
			."ON #__".$table_prefix."_product.product_tax_id = #__".$table_prefix."_tax_rate.tax_rate_id "
			."LEFT OUTER JOIN #__".$table_prefix."_product_discount "
			."ON #__".$table_prefix."_product.product_discount_id = #__".$table_prefix."_product_discount.discount_id "
			."WHERE #__".$table_prefix."_product.product_publish='Y'";
		
		if( CHECK_STOCK && PSHOP_SHOW_OUT_OF_STOCK_PRODUCTS != "1") {
			$query .= " AND product_in_stock > 0 ";
		}
		$query .= " ORDER BY #__".$table_prefix."_product.cdate DESC "
			."LIMIT 0, $max_items "
		;

		
		$db->setQuery($query);
		$rows = $db->loadObjectList();

		$i		= 0;
		$lists	= array();
		foreach ( $rows as $row )
		{
			$lists[$i] = $row;
			$lists[$i]->link = "index.php?page=shop.product_details&flypage=flypage.tpl&option=com_virtuemart&product_id=".$row->product_id;
			$lists[$i]->image = "components/com_virtuemart/show_image_in_imgtag.php?filename=".$row->product_full_image."&newxsize=".$params->get('thumb_width')."&newysize=".$params->get('thumb_height')."&fileout=";
			$lists[$i]->title = htmlspecialchars( $row->product_name );
			$lists[$i]->short_desc = htmlspecialchars( $row->product_s_desc);
			
			//Check if there is a discount
			$today=mktime(0,0,0,date("m, j, Y"));
			if($row->start_date <= $today && $row->end_date >= $today){
				
				
				$discount = ($row->is_percent)?($row->amount/100)*$row->product_price:$row->amount;
				$currentprice = $row->product_price - $discount; 
			} else{
				$currentprice = $row->product_price;
			}
			$oldprice=$row->product_price;
			//Include tax?
			if($params->get('show_tax')){
				$currentprice = $currentprice + $currentprice * $row->tax_rate;
				$oldprice = $row->product_price + $row->product_price * $row->tax_rate;
			}
			$lists[$i]->current_price = (round($currentprice,2) - intval(round($currentprice,2))==0)?intval(round($currentprice,2)):money_format("%2n", round($currentprice, 2));
			$lists[$i]->old_price = (round($oldprice,2) - intval(round($oldprice,2))==0)?intval(round($oldprice,2)):money_format("%2n", round($oldprice, 2));
			$i++;
		}


		
		if ($db->getErrorNum()) {
			JError::raiseWarning( 500, $db->stderr() );
		}
		return $lists;
	}
	
}

?>